﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Customers;

namespace MyMES.Writer.API.Application.CommendHandlers.Customers
{
    public class CreateCustomersCommendHandlers : IRequestHandler<CreateCustomersCommends, APIResult<Customermanagement>>
    {

        private readonly IBaseRepository<Customermanagement> customerRepository;
        private readonly IMapper mapper;

        public CreateCustomersCommendHandlers(IBaseRepository<Customermanagement> customerRepository, IMapper mapper)
        {
            this.customerRepository = customerRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<Customermanagement>> Handle(CreateCustomersCommends request, CancellationToken cancellationToken)
        {
            APIResult<Customermanagement> result = new APIResult<Customermanagement>();
            result.Code = APIEnum.添加成功;
            result.Msg = "添加成功";
            var department = mapper.Map<Customermanagement>(request);
            var sql = "INSERT into customermanagement VALUES(@CustomerId,@CustomerNo,@CustomerName,@Customerabbrevias,@CustomerEng,@CustomerTypeId,@Customerdescris,@CustomerAddress,@Customeroffadd,@Customeremadd,@Customerphone,@Customerlogo,@Contactone,@Contactphoneone,@Contactemailone,@Contacttwo,@Contactphonetwo,@Contactemailtwo,@SocialCredit,@Customerremark,@Isok,@IsDlete)";
            await customerRepository.Insert(department, sql);
            return result;
        }
    }
}
